#!/usr/bin/python
'''
Calculates various operations on two sets which are simple text files.
'''
import sys

# usage.
if len(sys.argv) == 0:
	print """
Usage: setops.py set1 set2 op

set1,set2 are text files with each element on a new line.

op is an operation to perform on the two sets:
difference: elements in set1 but not in set2
intersection: elements in set1 and also in set2
"""

# parameters.
set1_file = sys.argv[1]
set2_file = sys.argv[2]
op_type = sys.argv[3]

# build sets.
def build_set(fpath):
	# read in file.
	fin = open(fpath, "rb")
	lines = fin.readlines()
	fin.close()
	
	# build set.
	res = set()
	for line in lines:
		res.add(line.strip())
	
	return res
	
# get sets.
set1 = build_set(set1_file)
set2 = build_set(set2_file)

# perform appropriate op.
res = None
if op_type == "difference":
	res = set1.difference(set2)
	
elif op_type == "intersection":
	res = set1.intersection(set2)
	
# print result.
for r in res:
	print r

